#!/usr/bin/perl
use Data::Dumper;

use strict;
use warnings;

my %min_hash;
my %max_hash;
my $topdir="/home/houman/Desktop/isvc/draw3/";

opendir(DIR1, $topdir) or die $!;

while (my $filedraw = readdir(DIR1)) 
{
    
    # We only want files
    next unless (-d $topdir . $filedraw);

    # Use a regular expression to find files ending in .txt
    next unless ($filedraw =~ m/dir_/);

    my $newdir= $topdir . $filedraw ."/png/";
    
    opendir(DIR2, $newdir) or die $!;
    print "new directory is "  . $newdir . "\n";
    
    while (my $filepng = readdir(DIR2)) 
    {
	my $newfcomb= $newdir . $filepng;
	next unless (-f $newfcomb);
	next unless ($filepng =~ m/\.png/);


	if($filepng =~ m/WINNINGMIN_(.*)\.png/ )
	{
	    print "MIN $filepng \n";
	    if ( exists $min_hash{$1}) { $min_hash{$1}++;}
	    else {$min_hash{$1}=1;}
	    	    unless( exists $max_hash{$1}) { $max_hash{$1}=0;}
	}

	if($filepng =~ m/WINNINGMAX_(.*)\.png/)
	{
	    print "MAX $filepng \n";
	    if (exists $max_hash{$1}) {$max_hash{$1}++;}
	    else {$max_hash{$1}=1;}
	    	    unless( exists $min_hash{$1}) { $min_hash{$1}=0;}

	}


    }


closedir(DIR2);
}

closedir(DIR1);


print "sorting min hash \n";
foreach my $k (sort {$min_hash{$a} <=>  $min_hash{$b}} keys %min_hash) 
{
    print " $k has $min_hash{$k}  value for max hash is  $max_hash{$k}\n "; 
}


print "sorting max hash \n";
foreach my $k (sort {$max_hash{$a} <=>  $max_hash{$b}} keys %max_hash) 
{
    print " $k has $max_hash{$k} value for min hash is  $min_hash{$k} \n"; 
}
